﻿package app.view.components.guiDima.switchContent 
{
	import app.model.RemoteProxy;
	import app.view.components.guiDima.slideShowD.Image;
	import app.view.components.guiDima.switchContent.ContentBaseD;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.text.AntiAliasType;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;
	import flash.text.TextFormatAlign;
	/**
	 * Контейнер с текстом и картинкой
	 * @author Dmitryi_Koruagin, icq:585466579, dmitryikoruagin@mail.ru, skype: ok_ru1
	 */
	public class ContentTextWithImageD extends ContentBaseD
	{
		
		private var _field:TextField;
		private var _isOnlyText:Boolean=false;
		private var _isOnlyImage:Boolean=false;
		private var _image:Image;
		private var format:TextFormat;
		
		//	public
		
		//	private
		
		//	const
		private static const PROP_IMAGE_TEXT:Number = 0.7;
		private static const GAP_IMAGE_TEXT:Number = 10;
		
		//	CONSTRUCTOR
		public function ContentTextWithImageD(w:Number,h:Number) 
		{
			super(w,h);
			
		}
		
		//	METHODS
		//	private
		override public function init():void
		{
			super.init();
			
			// далее добавляем что нужно в соответствии контенту
			
			
			format = new TextFormat();
			format.size = 12;
			format.kerning = true;
			format.align = TextFormatAlign.JUSTIFY;
			format.font='_sans'//'Arial'
			
			
		}
		
		private function createImageWithText():void
		{
			
			_field = new TextField();
			_field.x = _gapLeft+(_contentWidth - _gapLeft - _gapRight)*(1-PROP_IMAGE_TEXT)+GAP_IMAGE_TEXT;
			_field.y = _gapTop;
			_field.wordWrap = true;
			_field.multiline = true;
			_field.width = (_contentWidth - _gapLeft - _gapRight-GAP_IMAGE_TEXT)*PROP_IMAGE_TEXT;
			_field.height = _contentHeight - _gapTop- _gapBottom;
			_field.mouseEnabled = false;
			_field.defaultTextFormat = format;
			//_field.embedFonts = true;
			_field.antiAliasType = AntiAliasType.ADVANCED;
			_field.htmlText = _data.text;
			//_field.autoSize = TextFieldAutoSize.LEFT;
			addChild(_field);
			
			_isOnlyText = true;
			
			//
			var iw:Number = (_contentWidth - _gapLeft - _gapRight - GAP_IMAGE_TEXT) * (1 - PROP_IMAGE_TEXT);
			var ih:Number = _contentHeight - _gapTop - _gapBottom;
			
			_image = new Image(iw, ih, Image.RESIZE_MIN, Image.ALIGN_CENTER, 0, 0, true, 100);
			addChild(_image);
			_image.x = _gapLeft;
			_image.y = _gapTop;
			_image.loadImage(RemoteProxy.DOMEN + _data.image);
			
			_isOnlyImage = true;
			
			
		}
		
		
		private function createOnlyText():void
		{
			
			
			_field = new TextField();
			_field.x = _gapLeft;
			_field.y = _gapTop;
			_field.wordWrap = true;
			_field.multiline = true;
			_field.width = _contentWidth - _gapLeft - _gapRight;
			_field.height = _contentHeight - _gapTop- _gapBottom;
			_field.mouseEnabled = false;
			_field.defaultTextFormat = format;
			//_field.embedFonts = true;
			_field.antiAliasType = AntiAliasType.ADVANCED;
			_field.htmlText = _data.text;
			//_field.autoSize = TextFieldAutoSize.LEFT;
			addChild(_field);
			
			_isOnlyText = true;
		}
		
		private function createOnlyImage():void
		{
			var iw:Number=_contentWidth - _gapLeft - _gapRight
			var ih:Number=_contentHeight - _gapTop- _gapBottom
			
			_image = new Image(iw, ih, Image.RESIZE_MIN, Image.ALIGN_CENTER, 0, 0, true, 100);
			addChild(_image);
			_image.x = _gapLeft;
			_image.y = _gapTop;
			_image.loadImage(RemoteProxy.DOMEN + _data.image);
			
			_isOnlyImage = true;
			
		}
		
		//	public
		override public function onDestroy():void
		{
			if (_isOnlyText==true) 
			{
				removeChild(_field);
			}
			
			if (_isOnlyImage==true) 
			{
				_image.destroy();
				removeChild(_image);
			}
			
			super.onDestroy();
		}
		//	----------------------------------------------------------------------------------
		
		//	GETTERS/SETTERS
		override public function get data():Object { return _data; };
		
		override public function set data(value:Object):void 
		{
			super.data = value;
			////trace('***',_data.text);
			////trace('---*',_data.text);
			////trace('+++*', _data.image);
			////trace('------++++------');
			
			if (_data.text!='' && _data.image!=null) 
			{
				// создаем текст и картинку
				createImageWithText();
			}else
			{
				if (_data.text!='') 
				{
					// создаем только текст
					createOnlyText();
					
				}
				if (_data.image!=null) 
				{
					// созаем только картинку
					createOnlyImage();
					
				}
				
			}
		};
		
		
		//	----------------------------------------------------------------------------------
		
		//	HANDLERS
		
	}

}